package com.alasbusy.alasbusyapp.activity;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import com.alasbusy.alasbusyapp.R;
import com.alasbusy.alasbusyapp.activity.fragment.*;
import com.alasbusy.alasbusyapp.activity.pic.PhotoPickerActivity;
import com.alasbusy.alasbusyapp.activity.pic.PicCutoutActivity;
import com.alasbusy.alasbusyapp.base.BaseFragmentActivity;
import com.alasbusy.highperformancefriendscirclelibrary.interfaces.OnPraiseOrCommentClickListener;
import com.tbruyelle.rxpermissions.RxPermissions;
import rx.functions.Action1;

/**
 * Created by Administrator on 2018/5/10.
 */

public class MainActivity extends BaseFragmentActivity implements OnPraiseOrCommentClickListener {

    private FragmentTabHost mTabHost;
    private View indicator = null;
    private ImageView mTab_image;
    private TextView mTab_title;
    private LayoutInflater layoutInflater;
    private Class fragmentArray[] = {HomeFragment.class, WebFragment.class, RewardFragment.class, SocialFragment.class, MySelfFragment.class};
    public static MainActivity instance;
    //定义数组来存放按钮图片
    private int mImageViewArray[] = {R.drawable.home_selector, R.drawable.web_mb_selector, R.drawable.reward_selector,R.drawable.social_selector, R.drawable.my_selector};

    //Tab选项卡的文字
    private String mTextviewArray[] = {"主页", "模板", "支持","社交", "我的"
           /* this.getResources().getString(R.string.home_title),
            this.getResources().getString(R.string.school_title),
            this.getResources().getString(R.string.family_title)*/};
    private View mTop;

    @Override
    protected void initView() {

        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.MEDIA_CONTENT_CONTROL,
                Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.CAMERA,
                Manifest.permission.CALL_PHONE,
                Manifest.permission.INTERNET,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE)
                .subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean granted) {
                        if (granted) {
                        } else {
                        }
                    }
                });


        instance = this;
        mTop = findViewById(R.id.top);
        setTop(mTop);
        layoutInflater = LayoutInflater.from(this);
        /*就是把指定的布局资源注射到指定的容器*/
        getTitle(mTextviewArray[0]);
        getVisibility(false, MainActivity.this);
        //实例化TabHost对象，得到TabHost
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        //R.id.realtabcontent 这里是一个FrameLayout标签
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        //得到fragment的个数
        int count = fragmentArray.length;
        for (int i = 0; i < count; i++) {
            //为每一个Tab按钮设置图标、文字和内容
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]).setIndicator(getTabItemView(i));
            //将Tab按钮添加进Tab选项卡中
            mTabHost.addTab(tabSpec, fragmentArray[i], null);
            if (fragmentArray[i] == HomeFragment.class) {
                mTop.setVisibility(View.GONE);
            }

            //设置Tab按钮的背景
            mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.color.fffef3);
        }
    }

    private View getTabItemView(int index) {
        View inflate = View.inflate(this, R.layout.tabhost_layout, null);
        mTab_image = (ImageView) inflate.findViewById(R.id.tab_image);
        mTab_title = (TextView) inflate.findViewById(R.id.tab_title);
        mTab_image.setImageResource(mImageViewArray[index]);
        mTab_title.setText(mTextviewArray[index]);
        return inflate;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                getTitle(tabId);
                getVisibility(false, MainActivity.this);
            }
        });
    }

    @Override
    public int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void onBackPressed() {
        new AlertDialog.Builder(this)
                .setCancelable(true)
                .setTitle("提示")
                .setMessage("是否退出程序?")
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }

                }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                MainActivity.this.finish();
            }
        }).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1001 && resultCode == RESULT_OK) {
            String resultPhotoUri = data.getStringExtra(PhotoPickerActivity.SELECT_RESULTS);
            if (resultPhotoUri == null) {
                return;
            }
            data.setClass(this, PicCutoutActivity.class);
            startActivityForResult(data, 1002);
        }
    }

    @Override
    public void onPraiseClick(int position) {

    }

    @Override
    public void onCommentClick(int position) {
        //Toast.makeText(this, "you click comment", Toast.LENGTH_SHORT).show();
        SocialFragment.getEmojiPanelView().showEmojiPanel();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
